    //
//  ConsoleController.m
//  ActiveDeck
//
//  Created by BigChicken on 1/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConsoleController.h"


@implementation ConsoleController
@synthesize consoleTextView;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
														 NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];

    NSFileManager *nsFM = [NSFileManager defaultManager];
    
	NSString *logPath;
    NSDate *latestDate;
    
    for (int i =1 ; i < 8 ; i++)
    {
        logPath = [NSString stringWithFormat:@"%@/Log/%@%d.log", documentsDirectory, @"console", i];
        if(![nsFM fileExistsAtPath:logPath])
        {
            logPath = [NSString stringWithFormat:@"%@/Log/%@%d.log", documentsDirectory, @"console", --i];
            break;
        }
        else 
        {
            NSDictionary *fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:logPath error:nil];
            NSDate *thisFileDate = [fileAttribs objectForKey:NSFileModificationDate];

            if(i==1)
                latestDate = thisFileDate;
            else if([thisFileDate compare:latestDate] == NSOrderedAscending)
            {
                logPath = [NSString stringWithFormat:@"%@/Log/%@%d.log", documentsDirectory, @"console", --i];
                NSLog(@"Less Match LogFile:%@", logPath);

                break;
            }
        }
    }
    NSLog(@"LogFile:%@", logPath);
	
	NSError *err;
	NSString *logText = [NSString stringWithContentsOfFile:logPath encoding:NSUTF8StringEncoding error:&err];
	
	consoleTextView.text = logText;
    [consoleTextView scrollRangeToVisible:NSMakeRange([logText length], 0)];
}


-(IBAction)closeDialog:(id)sender
{
	[self dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [consoleTextView release];
    [super dealloc];
}


@end
